<?php

/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2017/6/28
 * Time: 10:46
 */

namespace Admin\Controller;

use Admin\Service\AccountantService;

class AccountantController extends BaseController {


    public function __construct() {
        parent::__construct();
        $this->accService = new AccountantService();
        $this->assign('q_status',C('QUESTION_STATUS'));
        $this->assign('a_status',C('ANSWER_STATUS'));
        $this->assign('user_status',C('ACCOUNTING_STATUS'));
    }

    public function index() {
        $cat_ids = M('categories')->getField('id,cat_name');
        $this->assign('cat_ids',$cat_ids);
        $this->display();
    }
    public function ajax_index() {

        $nowPage = I('get.p', 1);
        $cat_ids = I('post.cat_ids', '');

        $user_name = I('post.username', '');
        $mobile = I('post.mobile', '');
        $status = I('post.status', '');
        $city = I('post.city', '');

        $list = $this->accService->accList($cat_ids,$user_name,$mobile,$status,$city,1);
        $page = $list['data'];

        $busConductList = $this->accService->accList($cat_ids,$user_name,$mobile,$status,$city,0,$nowPage);

        $this->assign('page', $page->show());
        $this->assign('lists', $busConductList['data']);
        $this->display();
    }



    public function detail() {
        $id = I('get.id');
        $info = $this->accService->accInfo($id);

        $province = M('region')->where('level = 1')->select();
        $this->assign('provinceList', $province);
        if($info['info']) {
            $city = M('region')->where('level = 2 and parent_id = ' . $info['info']['province'])->select();
            $this->assign('cityList', $city);
        }

        $cat_ids = M('categories')->getField('id,cat_name');
        $this->assign('cat_ids',$cat_ids);

        $this->assign('info', $info['info']);

        $this->assign('act','edit');
        $this->initEditor('head_pic');
        $this->display();
    }


    public function add() {
        $province = M('region')->where('level = 1')->select();
        $this->assign('provinceList', $province);
        $cat_ids = M('categories')->getField('id,cat_name');
        $this->assign('cat_ids',$cat_ids);
        $this->assign('act','add');
        $this->display('detail');
    }

    public function handle() {
        if (IS_POST){

            $data = I('post.');
            $res = $this->accService->handle($data);

            if ($res['code'] == 200 && $data['act'] != 'del'){

                $this->success($res['msg'] ? $res['msg'] : '操作成功', U('Admin/accountant/index'));
            } elseif($res['code'] != 200 && $data['act'] != 'del'){

                $this->error($res['msg'] ? $res['msg'] : '操作失败');
            } elseif ($data['act'] == 'del'){

                $this->ajaxReturn($res);
            }
        }
    }


    public function answer() {
        $cat_ids = M('categories')->getField('id,cat_name');
        $this->assign('cat_ids',$cat_ids);
        $this->display();
    }
    public function ajax_answer() {

        $au_id = I('get.id', 0);
        session('answer_id' , $au_id);
        $nowPage = I('get.p', 1);

        $cat_ids = I('post.cat_ids', '');
        $title = I('post.title', '');
        $status = I('post.status', '');

        $list = $this->accService->answerList($au_id,$title,$cat_ids,$status,1);
        $page = $list['data'];

        $busConductList = $this->accService->answerList($au_id,$title,$cat_ids,$status,0,$nowPage);

        $this->assign('page', $page->show());
        $this->assign('lists', $busConductList['data']);
        $this->display();
    }

    public function answer_detail() {

        $q_id = I('get.id', 0);

        $info = M('question')->where('id = '.$q_id)->find();
        if($info['answer_status'] == 2){
            $answer = M('answer')->where('question_id = '.$q_id)->find();
        }
        $this->assign('info',$info);
        $this->assign('answer',$answer);
        $this->assign('act','edit');
        $this->display();
    }

    public function answer_handle() {
        if (IS_POST){

            $data = I('post.');
            $res = $this->accService->answer_handle($data);

            if ($res['code'] == 200 && $data['act'] != 'del'){

                $this->success(  '操作成功', U('Admin/accountant/answer/id/'. session('answer_id')));
            } elseif($res['code'] != 200 && $data['act'] != 'del'){

                $this->error($res['msg'] ? $res['msg'] : '操作失败');
            } elseif ($data['act'] == 'del'){

                $this->ajaxReturn($res);
            }
        }
    }


}
